#!/bin/bash

# Nacos CLI 工具 - 通过 curl 命令操作 Nacos

NACOS_SERVER="http://localhost:8848"

# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color

# 显示帮助信息
show_help() {
    echo "Nacos CLI 工具"
    echo ""
    echo "用法: $0 <命令> [参数]"
    echo ""
    echo "配置管理命令:"
    echo "  publish-config <dataId> <group> <content>  - 发布配置"
    echo "  get-config <dataId> <group>                - 获取配置"
    echo "  delete-config <dataId> <group>             - 删除配置"
    echo ""
    echo "服务发现命令:"
    echo "  register <serviceName> <ip> <port>         - 注册服务"
    echo "  deregister <serviceName> <ip> <port>       - 注销服务"
    echo "  list-instances <serviceName>               - 查询服务实例"
    echo ""
    echo "健康检查命令:"
    echo "  health                                     - 检查服务健康状态"
    echo "  metrics                                    - 查看服务指标"
    echo ""
    echo "示例:"
    echo "  $0 publish-config example.properties DEFAULT_GROUP 'server.port=8080'"
    echo "  $0 get-config example.properties DEFAULT_GROUP"
    echo "  $0 register my-service 127.0.0.1 8080"
    echo ""
}

# 发布配置
publish_config() {
    local data_id=$1
    local group=$2
    local content=$3
    
    if [ -z "$data_id" ] || [ -z "$group" ] || [ -z "$content" ]; then
        echo -e "${RED}错误: 缺少参数${NC}"
        echo "用法: $0 publish-config <dataId> <group> <content>"
        exit 1
    fi
    
    response=$(curl -s -X POST "$NACOS_SERVER/nacos/v1/cs/configs" \
        -d "dataId=$data_id" \
        -d "group=$group" \
        -d "content=$content")
    
    if [ "$response" == "true" ]; then
        echo -e "${GREEN}✅ 配置发布成功${NC}"
    else
        echo -e "${RED}❌ 配置发布失败${NC}"
    fi
}

# 获取配置
get_config() {
    local data_id=$1
    local group=$2
    
    if [ -z "$data_id" ] || [ -z "$group" ]; then
        echo -e "${RED}错误: 缺少参数${NC}"
        echo "用法: $0 get-config <dataId> <group>"
        exit 1
    fi
    
    response=$(curl -s -X GET "$NACOS_SERVER/nacos/v1/cs/configs?dataId=$data_id&group=$group")
    
    if [ -n "$response" ]; then
        echo -e "${GREEN}配置内容:${NC}"
        echo "$response"
    else
        echo -e "${RED}❌ 配置不存在${NC}"
    fi
}

# 删除配置
delete_config() {
    local data_id=$1
    local group=$2
    
    if [ -z "$data_id" ] || [ -z "$group" ]; then
        echo -e "${RED}错误: 缺少参数${NC}"
        echo "用法: $0 delete-config <dataId> <group>"
        exit 1
    fi
    
    response=$(curl -s -X DELETE "$NACOS_SERVER/nacos/v1/cs/configs?dataId=$data_id&group=$group")
    
    if [ "$response" == "true" ]; then
        echo -e "${GREEN}✅ 配置删除成功${NC}"
    else
        echo -e "${RED}❌ 配置删除失败${NC}"
    fi
}

# 注册服务
register_service() {
    local service_name=$1
    local ip=$2
    local port=$3
    
    if [ -z "$service_name" ] || [ -z "$ip" ] || [ -z "$port" ]; then
        echo -e "${RED}错误: 缺少参数${NC}"
        echo "用法: $0 register <serviceName> <ip> <port>"
        exit 1
    fi
    
    response=$(curl -s -X POST "$NACOS_SERVER/nacos/v1/ns/instance" \
        -d "serviceName=$service_name" \
        -d "ip=$ip" \
        -d "port=$port")
    
    if [ "$response" == "ok" ]; then
        echo -e "${GREEN}✅ 服务注册成功${NC}"
    else
        echo -e "${RED}❌ 服务注册失败: $response${NC}"
    fi
}

# 注销服务
deregister_service() {
    local service_name=$1
    local ip=$2
    local port=$3
    
    if [ -z "$service_name" ] || [ -z "$ip" ] || [ -z "$port" ]; then
        echo -e "${RED}错误: 缺少参数${NC}"
        echo "用法: $0 deregister <serviceName> <ip> <port>"
        exit 1
    fi
    
    response=$(curl -s -X DELETE "$NACOS_SERVER/nacos/v1/ns/instance" \
        -d "serviceName=$service_name" \
        -d "ip=$ip" \
        -d "port=$port")
    
    if [ "$response" == "ok" ]; then
        echo -e "${GREEN}✅ 服务注销成功${NC}"
    else
        echo -e "${RED}❌ 服务注销失败: $response${NC}"
    fi
}

# 查询服务实例
list_instances() {
    local service_name=$1
    
    if [ -z "$service_name" ]; then
        echo -e "${RED}错误: 缺少参数${NC}"
        echo "用法: $0 list-instances <serviceName>"
        exit 1
    fi
    
    response=$(curl -s -X GET "$NACOS_SERVER/nacos/v1/ns/instance/list?serviceName=$service_name")
    
    echo -e "${GREEN}服务实例列表:${NC}"
    echo "$response" | jq '.' 2>/dev/null || echo "$response"
}

# 健康检查
health_check() {
    response=$(curl -s -X GET "$NACOS_SERVER/nacos/v1/console/health/readiness")
    
    echo -e "${GREEN}健康状态:${NC}"
    echo "$response"
}

# 查看指标
metrics() {
    response=$(curl -s -X GET "$NACOS_SERVER/nacos/v1/console/server/state")
    
    echo -e "${GREEN}服务器状态:${NC}"
    echo "$response"
}

# 主命令处理
case "$1" in
    publish-config)
        publish_config "$2" "$3" "$4"
        ;;
    get-config)
        get_config "$2" "$3"
        ;;
    delete-config)
        delete_config "$2" "$3"
        ;;
    register)
        register_service "$2" "$3" "$4"
        ;;
    deregister)
        deregister_service "$2" "$3" "$4"
        ;;
    list-instances)
        list_instances "$2"
        ;;
    health)
        health_check
        ;;
    metrics)
        metrics
        ;;
    help|--help|-h)
        show_help
        ;;
    *)
        echo -e "${RED}错误: 未知命令 '$1'${NC}"
        echo ""
        show_help
        exit 1
        ;;
esac

